package com.example.myapplication2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SimpleTimeZone;

public class MainActivity extends AppCompatActivity {

    private String selectColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        //initView();
        //initList();
        initMultiList();
    }

    private void initMultiList(){
        //1.xml中添加复合控件
        ListView listView = findViewById(R.id.list_View);
        //2.构造数据
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> item = new HashMap<>();
        item.put("item_name","张三");
        item.put("item_icon",R.mipmap.ic_launcher);

        data.add(item);
        /*for(int i =0;i<20;i++){
            Map<String,Object> item = new HashMap<>();
            item.put("item_name","张三"+i);
            item.put("item_icon",R.mipmap.ic_launcher);
            data.add(item);
        }*/
        //3.构造条目布局list_item_contact.xml
        //4.构造适配器
        //参数4：数据List中Map的key对应的值
        //参数5：条目布局内的子控件的id数组
        //参数6：key取出的值赋值给条目布局中的对应的控件id
        SimpleAdapter adapter = new SimpleAdapter(this,
                data,R.layout.list_item_contact,
                new String[]{"item_name","item_icon"},
                new int[]{R.id.item_contact_name,R.id.item_contact_icon});
        listView.setAdapter(adapter);
        /*
    }
    private void initList(){
        //1.xml中添加复合控件
        ListView listView = findViewById(R.id.list_View);
        //2.构造数据
        List<String> data = new ArrayList<String>();
        for(int i =0;i<20;i++){
            data.add("联系人 张三"+i);

        }
        //3.创建列表条目布局 list_item_text.xml
        //4.构建适配器adapter
        ArrayAdapter adapter = new ArrayAdapter(
                this,R.layout.list_item_text,data
        );
        //5.将适配器adapter赋值给listView
        listView.setAdapter(adapter);
    }


    private  void initView(){
        Spinner colorSpiner = findViewById(R.id.string001);
        List<String> list = new ArrayList<String>();
        list.add("选择颜色");
        list.add("红色|Red");
        list.add("黄色|Yellow");
        list.add("绿色|Green");
        list.add("蓝色|Blue");
        list.add("橙色|Orange");
        //参数2：下拉列表条目
        //参数3：动态数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item,list);
        //给下拉列表设置列表条目布局
        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        colorSpiner.setAdapter(adapter);


        //参数2：strings.xml中的数据
        //参数3：Spinner头部布局，当前使用系统布局android.R.layout.simple_spinner_dropdown_item
      *//*  ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this,R.array.Str_color,
                android.R.layout.simple_spinner_dropdown_item);*//*
        //给下拉列表设置列表条目布局
        *//*adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item
        );*//*
        //colorSpiner.setAdapter(adapter);
        //设置是否显示控件，对应属性为android：visible
        colorSpiner.setVisibility(View.VISIBLE);

        colorSpiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

                if(position!=0 || !TextUtils.isEmpty(selectColor)){
                   *//*Toast.makeText(SpinnerActivity.this,"选中了："+
                                   getResources().getStringArray(R.array.Str_color)[position],
                           Toast.LENGTH_SHORT).show();*//*
                    Toast.makeText(MainActivity.this,"选中了："+
                                    list.get(position),
                            Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
*/
    }
}